package com.app.system.config.jwt;

import com.app.system.utils.dataType.StringUtils;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Date;

/**
 * jwt工具类
 * Created by WCF on 2020/5/10.
 */
@SuppressWarnings("all")
public class JwTUtils {

    /**
     * 校验token
     *
     * @param token
     * @param username
     * @param password
     * @return
     */
    public static boolean verify(String token, String username, String password) {
        try {
            boolean expirest = isExpirest(token);
            Algorithm algorithm = Algorithm.HMAC256(password);
            JWTVerifier verifier = JWT.require(algorithm).withClaim("username", username).build();
            DecodedJWT jwt = verifier.verify(token);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 生成签名
     *
     * @param username
     * @param password
     * @return
     */
    public static String sign(String username, String password) {
        try {
            long expTime = 365 * 24 * 60 * 60 * 1000;//7天过期时间
            long currentTimes = System.currentTimeMillis();
            Date date = new Date(currentTimes + expTime);
            Algorithm algorithm = Algorithm.HMAC256(password);
            return JWT.create()
                    .withClaim("username", username)
                    .withExpiresAt(date)
                    .withIssuedAt(new Date(currentTimes))
                    .sign(algorithm);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取用户名
     *
     * @param token
     * @return
     */
    public static String getUsername(String token) {
        try {
            DecodedJWT jwt = getDecodedJWT(token);
            return jwt.getClaim("username").asString();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 是否过期
     * false：未过期，true：过期
     *
     * @param token
     * @return
     */
    public static boolean isExpirest(String token) {
        try {
            Date date = getExpirestAt(token);
            if (date == null) {
                return true;
            } else {
                if (date.getTime() < System.currentTimeMillis()) {//最大时间小于当前时间，过期
                    return true;
                } else {
                    return false;
                }
            }
        } catch (Exception e) {
            return true;
        }
    }

    /**
     * 获取过期时间
     *
     * @param token
     * @return
     */
    public static Date getExpirestAt(String token) {
        try {
            DecodedJWT jwt = getDecodedJWT(token);
            return jwt.getExpiresAt();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 返回null：非合法token、非null：合法token
     *
     * @param token
     * @return
     */
    public static DecodedJWT getDecodedJWT(String token) {
        if (StringUtils.isEmpty(token)) {
            return null;
        }
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt;
        } catch (Exception e) {
            return null;
        }
    }
}
